package tps12.axwax.client.view;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.HasOpenHandlers;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;

public class ClueList extends Composite implements HasOpenHandlers<Clue> {
	interface MyUiBinder extends UiBinder<FlowPanel, ClueList> {}
	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
	
	interface Style extends CssResource {
		String text();
	}
	
	@UiField Style style;
	
	public ClueList(String label, Collection<String> clues) {
		initWidget(uiBinder.createAndBindUi(this));
		
		this.label.setText(label);
		final ClueList list = this;
		for (String c : clues) {
			final Clue clue = new Clue(c);
			clue.addOpenHandler(new OpenHandler<Clue>() {
				@Override
				public void onOpen(OpenEvent<Clue> event) {
					OpenEvent.fire(list, event.getTarget());
				}	
			});
			panel.add(clue);
			this.clues.add(clue);
		}
	}
	
	@UiField FlowPanel panel;
	@UiField Label label;
	
	List<Clue> clues = new ArrayList<Clue>();

	@Override
	public HandlerRegistration addOpenHandler(OpenHandler<Clue> handler) {
		return addHandler(handler, OpenEvent.getType());
	}

	public void closeExcept(Clue target) {
		for (Clue clue : clues)
			if (clue != target)
				clue.close();
	}
}